package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runen.infinitemovement.R;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class TeamPopupWindow extends PopupWindow {

    private Context context;
    private View layout;
    private TextView tv_change_team, tv_transfer, tv_add, tv_manager;
    private boolean isLeader;
    private OnClick onClick;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public static TeamPopupWindow getInstance(Context context, boolean isLeader) {
        return new TeamPopupWindow(context, isLeader);
    }

    private TeamPopupWindow(Context context, boolean isLeader) {
        super(context);
        this.context = context;
        this.isLeader = isLeader;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_team, null);
        tv_change_team = layout.findViewById(R.id.tv_change_team);
        tv_transfer = layout.findViewById(R.id.tv_transfer);
        tv_add = layout.findViewById(R.id.tv_add);
        tv_manager = layout.findViewById(R.id.tv_manager);

        if (isLeader) {
            tv_add.setVisibility(View.GONE);
            tv_manager.setVisibility(View.VISIBLE);
        } else {
            tv_transfer.setVisibility(View.GONE);
        }
    }


    private void initEvent() {
        tv_change_team.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.change();
                }
            }
        });
        tv_transfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.transfer();
                }
            }
        });
        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.add();
                }
            }
        });
        tv_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null)
                    onClick.manager();
            }
        });
    }


    public void show(View view) {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAsDropDown(view, Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface OnClick {
        void change();

        void transfer();

        void add();

        void manager();
    }
}
